#include <bits/stdc++.h>
using namespace std;
//scanf()  printf 可以满分
const int INF = 0x3f3f3f3f;
const int maxn = 5e5 + 2;
typedef long long LL;
int son[maxn][20];
LL dis[maxn][20];
int maxw[maxn][20];
int t[maxn], w[maxn];

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  int n, k;
  // cin >> n >> k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    // cin >> t[i];
    scanf("%d", &t[i]);
    son[i][0] = t[i];
  }
  for (int i = 1; i <= n; ++i) {
    // cin >> w[i];
    scanf("%d", &w[i]);
    // dis[i][0] = w[i];
    maxw[i][0] = w[i];
  }

  for (int j = 1; (1LL << j) <= k; ++j) {
    for (int i = 1; i <= n; ++i) {
      int nx = son[i][j - 1];
      son[i][j] = son[nx][j - 1];
      // dis[i][j] = dis[i][j - 1] + dis[nx][j - 1];
      maxw[i][j] = max(maxw[i][j - 1], maxw[nx][j - 1]);
      // cout << i << " " << j << " " << son[i][j] << " " << dis[i][j] << endl;
    }
  }

  for (int i = 1; i <= n; ++i) {
    // LL len = 0;
    int ans = INT_MIN;
    int p = i;
    for (int j = 0; (1LL << j) <= k; ++j) {
      if (k & (1 << j)) {
        // len += dis[p][j];
        ans = max(ans, maxw[p][j]);
        p = son[p][j];
      }
    }
    // cout << ans << endl;
    printf("%d\n", ans);
  }

  return 0;
}
